package handlers

import (
	"log"
	"net/http"

	"gitee.com/anxu/golang-web/src/models"
	"gitee.com/anxu/golang-web/src/repository"
)

type Post struct {
	hander
}

func (this *Post) Create(w http.ResponseWriter, r *http.Request) {
	user := this.user()
	if user == nil {
		this.redirect("/login", http.StatusFound, w, r)
		return
	}

	body := r.FormValue("body")
	uuid := r.FormValue("uuid")

	thread := repository.Thread.FindByUuid(uuid)

	post := &models.Post{
		Body:   body,
		User:   *user,
		Thread: thread,
	}

	if body != "" && uuid != "" {
		log.Println("post ", post)
		repository.Post.Create(post)
	}
	this.redirect(r.URL.Path, http.StatusFound, w, r)
}
